<?php

namespace app\admin\command;

use think\addons\AddonException;
use think\addons\Service;
use think\Config;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Db;
use think\Exception;
use think\exception\PDOException;

class Check extends Command
{

    protected function configure()
    {
        $this->setName('check');
    }

    protected function execute(Input $input, Output $output)
    {
        // Db::table('check_key')->where('1=1')->update(['status'=>null]);die;
        
        $list = Db::table('check_key')->where('status','')->limit(100)->select();
            
        foreach($list as $v){
            $status = self::http_curl_get($v['key']);
            Db::table('check_key')->where('key',$v['key'])->update(['status'=>$status]);
        }
    }
    
    public function http_curl_get($code) {
        $type=1;
        $url='https://autochess.ppbizon.com/cdkey/get/@';
        $curl = curl_init();
        curl_setopt($curl,CURLOPT_TIMEOUT,5000);
        curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,false);
        // curl_setopt($curl,CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
        curl_setopt($curl,CURLOPT_URL,$url.$code);
        if($type == 1){
            curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
        }
        $res = curl_exec($curl);
        if($res){
            curl_close($curl);
            $str = $res;
            return mb_strpos($str, '已被激活') ? '已被激活':'未激活';
        }else {
            $error = curl_errno($curl);
            curl_close($curl);
            return $error;
        }
    }

}
